/**
 * 
 */
package fhq.hcmute.qrcode.detector;

/**
 * @author Hien Nguyen Le
 * @Email: nglehien@gmail.com
 */
public class PatternPoint {
	private final float x;
	private final float y;
	private final float moduleSize;
	private int count;

	public float getX() {
		return x;
	}

	public float getY() {
		return y;
	}

	public PatternPoint(float x, float y, float moduleSize) {
		this(x, y, moduleSize, 1);
	}

	public PatternPoint(float x, float y, float moduleSize, int count) {
		this.x = x;
		this.y = y;
		this.moduleSize = moduleSize;
		this.count = count;
	}

	public float getModuleSize() {
		return moduleSize;
	}

	public int getCount() {
		return count;
	}

	@Override
	public boolean equals(Object obj) {
		PatternPoint myPoint = (PatternPoint) obj;
		if ((Math.abs((this.x - myPoint.x)) <= this.moduleSize / 2)
				&& (Math.abs((this.y - myPoint.y)) <= this.moduleSize / 2)) {
			return true;
		}
		return false;
	}

	public PatternPoint combine(PatternPoint point) {
		int combineCount = count + 1;
		float combinedX = (count * this.x + point.x) / combineCount;
		float combinedY = (count * this.y + point.y) / combineCount;
		float combinedModuleSize = (count * this.moduleSize + point.moduleSize)
				/ combineCount;
		return new PatternPoint(combinedX, combinedY, combinedModuleSize,
				combineCount);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return String.format("{X=%f, Y=%f, Module Size=%f}", x, y, moduleSize);
	}
}
